package org.balthie.cal.impl.cmd.spec;

import java.util.Deque;

import org.balthie.cal.common.ex.CalculatorException;
import org.balthie.cal.common.ex.InputException;
import org.balthie.cal.core.ICommand;

public class CmdSizeSpecs {

	public static final CmdSizeSpec EMPTY = new CmdSizeSpec(0);

	public static class CmdSizeSpec extends AbsCmdsSpec {

		private Integer expect;

		private CmdSizeSpec(int expectSize) {
			this.expect = expectSize;
		}
		
		@Override
		public void check(Deque<ICommand> candidate) throws CalculatorException {
			this.doCheck(candidate, Boolean.TRUE);
		}

		@Override
		protected boolean doCheck(Deque<ICommand> candidate, boolean isThrow) {
			Boolean isSatisfied = (expect.equals(candidate.size()));
			if (!isSatisfied && isThrow) {
				throw new InputException(String.format("ERROR: %s [expect size= %s] [actually= %s]",
						this.getClass().getSimpleName(), this.expect, candidate.size()));
			} else {
				return isSatisfied;
			}
		}
	}
}
